package com.mdc.mansion.core.modules.dishes.impl;
import com.google.common.collect.Lists;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.core.common.translate.TranslateLanguage;
import com.mdc.mansion.core.common.translate.TranslateService;
import com.mdc.mansion.core.common.util.RestaurantSupport;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesService;
import com.mdc.mansion.core.modules.dishes.dto.RestaurantDishesCategoryDTO;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishes;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;
import com.mdc.mansion.dataaccess.modules.system.entity.User;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mdc.mansion.dataaccess.common.service.CrudService;

import com.mdc.mansion.dataaccess.modules.dishes.dao.RestaurantDishesCategoryDao;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishesCategory;

import com.mdc.mansion.core.modules.dishes.RestaurantDishesCategoryService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 餐厅菜品分类信息ServiceImpl
 * @author mingdongc
 * @version 2018-01-01
 */
@Service
public class RestaurantDishesCategoryServiceImpl extends CrudService<RestaurantDishesCategoryDao, RestaurantDishesCategory> implements RestaurantDishesCategoryService {

    @Autowired
    private TranslateService translateService;

    @Autowired
    private RestaurantDishesService restaurantDishesService;
    @Override
    public List<RestaurantDishesCategoryDTO> currentUserRestaurantDishesCategory() {
        Restaurant restaurant = ContextHolder.currentRestaurant();
        RestaurantDishesCategory restaurantDishesCategory=new RestaurantDishesCategory();
        restaurantDishesCategory.setRestaurantId(restaurant.getId());
        Page<RestaurantDishesCategory> page = new Page<>();
        page.setOrderBy("sort_num");
        page.setPage(false);
        restaurantDishesCategory.setPage(page);
        List<RestaurantDishesCategory> list = this.findList(restaurantDishesCategory);
        List<RestaurantDishesCategoryDTO> restaurantDishesCategoryDTOs= Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(list)){
            for(RestaurantDishesCategory category:list){
                RestaurantDishesCategoryDTO dishesCategoryDTO=new RestaurantDishesCategoryDTO();
                BeanUtils.copyProperties(category,dishesCategoryDTO);
                restaurantDishesCategoryDTOs.add(dishesCategoryDTO);
            }
        }
        return restaurantDishesCategoryDTOs;
    }

    @Override
    @Transactional
    public void saveCurrentUserRestaurantDishesCategory(RestaurantDishesCategoryDTO restaurantDishesCategoryDTO) {
        restaurantDishesCategoryDTO.verfiy();
        RestaurantDishesCategory category=new RestaurantDishesCategory();
        BeanUtils.copyProperties(restaurantDishesCategoryDTO,category);
        User operationUser = RestaurantSupport.getCurrentOperationUser();
        category.setUpdateBy(operationUser);
        category.setRestaurantId(ContextHolder.currentRestaurant().getId());
        String name=restaurantDishesCategoryDTO.getName();
        category.setChineseName(translateService.translate(name, TranslateLanguage.CHINA));
        category.setEnglishName(translateService.translate(name, TranslateLanguage.ENGLISH));
        category.setName(translateService.translate(name, TranslateLanguage.INDONESIA));
        if(StringUtils.isNotBlank(category.getId())){
            this.updateSelective(category);
        }else{
            category.setCreateBy(operationUser);
            this.save(category);
        }

    }

    @Override
    @Transactional
    public void sort(List<RestaurantDishesCategoryDTO> restaurantDishesCategoryDTOList) {
        if(CollectionUtils.isEmpty(restaurantDishesCategoryDTOList)){return;}
        User operationUser = RestaurantSupport.getCurrentOperationUser();
        for(RestaurantDishesCategoryDTO categoryDTO:restaurantDishesCategoryDTOList){
            RestaurantDishesCategory restaurantDishesCategory=new RestaurantDishesCategory();
            restaurantDishesCategory.setId(categoryDTO.getId());
            restaurantDishesCategory.setSortNum(categoryDTO.getSortNum());
            restaurantDishesCategory.setUpdateBy(operationUser);
            this.updateSelective(restaurantDishesCategory);
        }
    }

    @Override
    @Transactional
    public void deleteById(String id) {
        RestaurantDishesCategory restaurantDishesCategory = this.get(id);
        if(restaurantDishesCategory==null){return;}
        RestaurantDishes dishes = new RestaurantDishes();
        dishes.setRestaurantDishesCategoryId(id);
        List<RestaurantDishes> restaurantDishesServiceList = restaurantDishesService.findList(dishes);
        if(CollectionUtils.isNotEmpty(restaurantDishesServiceList)){
            throw new BusinessException(I18nUtil.getMessage("dishes.category.delete.error.dishesexist"));
        }
        this.delete(restaurantDishesCategory);
    }
}
